package com.hqhy.like.front.order.dao.persist.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqhy.like.common.pojo.vo.PageData;
import com.hqhy.like.common.util.PageInfoToPageDataConverter;
import com.hqhy.like.front.order.dao.persist.mapper.OrderMapper;
import com.hqhy.like.front.order.dao.persist.repository.IOrderRepository;
import com.hqhy.like.front.order.pojo.entity.Order;
import com.hqhy.like.front.order.pojo.param.OrderQueryParam;
import com.hqhy.like.front.order.pojo.param.OrderSearchParam;
import com.hqhy.like.front.order.pojo.vo.OrderStandardVO;
import com.hqhy.like.front.order.pojo.vo.OrderTableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 处理订单数据的存储库实现类
 */
@Repository
@Slf4j
public class OrderRepositoryImpl implements IOrderRepository {

    @Autowired
    private OrderMapper orderMapper;

    public OrderRepositoryImpl() {
        log.info("创建存储库对象：OrderRepositoryImpl");
    }
    @Override
    public int insert(Order order) {
        log.debug("开始处理【插入订单】的数据访问层，订单数据类：{}",order);
        return orderMapper.insert(order);
    }

    @Override
    public int deleteById(Long orderId) {
        log.debug("开始处理【删除订单】的数据访问层，订单id：{}",orderId);
        return orderMapper.deleteById(orderId);
    }

    @Override
    public int updateById(Order order) {
        log.debug("开始处理【修改订单数据】的数据访问层，订单数据类: {}", order);
        return orderMapper.updateById(order);
    }

    @Override
    public OrderStandardVO getStandardById(Long orderId) {
        log.debug("开始处理【根据订单id查询订单】的数据访问层，订单id: {}", orderId);
        return orderMapper.getStandardById(orderId);
    }

    @Override
    public OrderTableVO getStandardByOrderNo(String orderNo) {
        log.debug("开始处理【根据订单编号查询订单桌台数据】的数据访问层，订单编号: {}", orderNo);
        return orderMapper.getStandardByOrderNo(orderNo);
    }

    @Override
    public PageData<OrderStandardVO> listByStateAndMethod(OrderSearchParam orderSearchParam) {
        log.debug("开始处理【根据下单方式和订单状态查询订单】的数据访问层，查询订单详情参数类: {}", orderSearchParam);
        Integer pageSize = orderSearchParam.getPageSize();
        Integer pageNum = orderSearchParam.getPageNum();
        PageHelper.startPage(pageNum, pageSize);
        List<OrderStandardVO> list = orderMapper.listByStateAndMethod(orderSearchParam);
        PageInfo<OrderStandardVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }
    @Override
    public PageData<OrderTableVO> listOrderForTable(OrderQueryParam orderQueryParam) {
        log.debug("开始处理【查询订单桌台数据】的数据访问层，查询订单桌台信息参数类: {}", orderQueryParam);
        Integer pageSize = orderQueryParam.getPageSize();
        Integer pageNum = orderQueryParam.getPageNum();
        PageHelper.startPage(pageNum, pageSize);
        List<OrderTableVO> list = orderMapper.listOrderForTable(orderQueryParam);
        PageInfo<OrderTableVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<OrderStandardVO> listAllCheckByMethod(Integer pageNum, Integer pageSize, Integer orderMethod) {
        log.debug("开始处理【根据下单方式查询所有确认订单页面的订单】的数据访问层，下单方式: {}，页码: {}，每页记录数：{}", orderMethod, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<OrderStandardVO> list = orderMapper.listAllCheckByMethod(orderMethod);
        PageInfo<OrderStandardVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<OrderStandardVO> searchByCondition(OrderSearchParam orderSearchParam) {
        log.debug("开始处理【根据条件查询订单】的数据访问层，查询订单详情参数类: {}", orderSearchParam);
//        HashMap<Object, Object> hashMap = new HashMap<>();
//        hashMap.put("orderMethod", orderMethod);
//        hashMap.put("orderState",orderState);
//        List<OrderStandardVO> list = null;
//        switch (searchMethod) {
//            case "订单号":
//                hashMap.put("orderNo", keyword);
//                list = orderMapper.searchByKeyword(hashMap);
//                break;
//            case "桌台号":
//                hashMap.put("tableName", keyword);
//                list = orderMapper.searchByKeyword(hashMap);
//                break;
//            case "电话号码":
//                hashMap.put("customerPhone", keyword);
//                list = orderMapper.searchByKeyword(hashMap);
//                break;
//            case "用户名":
//                hashMap.put("customerUsername", keyword);
//                list = orderMapper.searchByKeyword(hashMap);
//                break;
//        }
        String[] dateRange = orderSearchParam.getDateRange();
        if (dateRange != null && dateRange.length > 0){
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime startTime = LocalDate.parse(dateRange[0], pattern).atStartOfDay();
            LocalDateTime endTime = LocalDate.parse(dateRange[1], pattern).plusDays(1).atStartOfDay();
            orderSearchParam.setStartTime(startTime);
            orderSearchParam.setEndTime(endTime);
        }
        PageHelper.startPage(orderSearchParam.getPageNum(), orderSearchParam.getPageSize());
        List<OrderStandardVO> list = orderMapper.searchByCondition(orderSearchParam);
        PageInfo<OrderStandardVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

}
